<%@ page import="java.math.BigInteger" %>
<%@ page import="java.util.Scanner" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.io.*" %><%--
  Created by IntelliJ IDEA.
  User: 迷路的麋鹿
  Date: 2018/4/12
  Time: 19:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%!

    BigInteger count=null;
    public BigInteger load(File file){
        BigInteger count=null;
        if (!file.exists()){
            return count;
        }
            try {

                Scanner scanner = new Scanner(new FileInputStream(file));
                if (scanner.hasNext()) {
                    count = new BigInteger(scanner.next());
                }else{
                    count=new BigInteger("0");
                    save(file,count);
                }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return count;
    }

    public void save(File file,BigInteger count){
        try {
            PrintStream printStream=new PrintStream(file);
            printStream.print(count);
            printStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
%>
<%=count%><br/>
<%

    String path=application.getRealPath("/")+"count.txt";
    if (session.isNew()){
        out.print("新用户哟！");
        synchronized (this){
            File file=new File(path);
            count=load(file);
            count=count.add(new BigInteger("1"));
            save(file,count);
        }
    }

%>
<%=count==null?0:count%>
</body>
</html>
